import cv2
import os
import copy
data_set1=r"C:\\Users\\ASUS\\Desktop\\Atest_Data"
names=os.listdir(data_set1)
print(names)
data_set2=r"C:\\Users\\ASUS\\Desktop\\Atest_Data_added"
if not os.path.exists(data_set2): os.mkdir(data_set2) 
for name in names:
    if name.split(".")[1]=="jpg" or name.split(".")[1]=="png":
        print(name)
        img=cv2.imread(data_set1+"/"+name)
        img2=cv2.flip(img,1)
        img3=cv2.flip(img,0)
        img4=cv2.flip(img,-1)
        img_1 = cv2.transpose(img)    # 转置（行列互换）
        img_2=cv2.flip(img_1,1)
        img_3=cv2.flip(img_1,0)
        img_4=cv2.flip(img_1,-1)
        all_img=[img,img2,img3,img4,img_1,img_2,img_3,img_4]
        num=1
        for j in all_img:
            print(data_set2+"/"+name.split(".")[0]+"_"+str(num)+".jpg")
            cv2.imwrite(data_set2+"/"+name.split(".")[0]+"_"+str(num)+".jpg",j)
            num+=1
            
    if name.split(".")[1]=="txt":
        with open(data_set1+"/"+name,"r") as f:
            txt=f.readlines()
        info=[]
        for index,i in enumerate(txt):
            info.append({"class":i.split(" ")[0],"p":[float(i.split(" ")[1]),float(i.split(" ")[2]),float(i.split(" ")[3]),float(i.split(" ")[4][0:-1])]})

        info2=copy.deepcopy(info)
        for i in info2:
            i["p"][0]=1-i["p"][0]
        info3=copy.deepcopy(info)
        for i in info3:
            i["p"][1]=1-i["p"][1]
        info4=copy.deepcopy(info)
        for i in info4:
            i["p"][1]=1-i["p"][1]    
            i["p"][0]=1-i["p"][0]   
        # 旋转90度
        info_1=copy.deepcopy(info)
        for i in info_1:
            temp=i["p"][0]   
            i["p"][0] =i["p"][1]   
            i["p"][1] =temp
            temp=i["p"][2]   
            i["p"][2] =i["p"][3]   
            i["p"][3] =temp  
        info_2=copy.deepcopy(info_1)
        for i in info_2:
            i["p"][0]=1-i["p"][0]
        info_3=copy.deepcopy(info_1)
        for i in info_3:
            i["p"][1]=1-i["p"][1]
        info_4=copy.deepcopy(info_1)
        for i in info_4:
            i["p"][1]=1-i["p"][1]    
            i["p"][0]=1-i["p"][0]   
        all_info=[info,info2,info3,info4,info_1,info_2,info_3,info_4]
        num=1
        for one_info in all_info:
            str1=""
            for j in one_info:
                str1+=j["class"]+" "'%.6f'%j["p"][0]+" "+'%.6f'%j["p"][1]+" "+'%.6f'%j["p"][2]+" "+'%.6f'%j["p"][3]+"\n"
            with open(data_set2+"/"+name.split(".")[0]+"_"+str(num)+".txt","w") as f:
                f.write(str1)
            num+=1
